import { createRouter, createWebHistory } from 'vue-router'
import Login from '../pages/Login/Login.vue'
import Register from '../pages/Login/Register.vue'
import ResetPwd from '../pages/Login/ResetPwd.vue'
import PickUpSite from '../pages/PickUpSite/PickUpSite.vue'
import Main from '../pages/Main/Main.vue'
import Index from '../pages/Main/Index/Index.vue'
import Detail from '../pages/Main/Index/detail.vue'
import Cart from '../pages/Main/Cart/Cart.vue'
import ConfirmOrder from '../pages/Main/Cart/confirmOrder.vue'
import Mine from '../pages/Main/Mine/Mine.vue'
import Personal from '../pages/Main/Mine/personal.vue'
import FinishOrder from '../pages/Main/Mine/finishOrder.vue'
import Agreement from '../pages/Main/Mine/agreement.vue'
import Order from '../pages/Main/Order/Order.vue'



// axios
import axios from 'axios'

// 引入store的实例，相当于页面中的this.$store
import store from '../store/index.js'



const routes = [
    {
        path: '/login',
        component: Login,
        meta: { needLogin: false },
    },
    {
        path: '/register',
        component: Register,
        meta: { needLogin: false },
    },
    {
        path: '/resetPwd',
        component: ResetPwd,
        meta: { needLogin: false },
    },
    {
        path: '/pickupsite',
        component: PickUpSite,
        meta: { needLogin: true},
    },
    {
        path: '/',
        component: Main,
        meta: { needLogin: true },
        redirect: '/index',
        children: [
            {
                path: '/index',
                component: Index,
                meta: { needLogin: true, index: 0 },

            }, {
                path: '/detail',
                component: Detail,
                meta: { needLogin: true, index: 0 },
            
            }, {
                path: '/cart',
                component: Cart,
                meta: { needLogin: true, index: 1 },

            }, {
                path:'/confirmOrder',
                component: ConfirmOrder,
                meta: { needLogin: true, index: 1 },
            }, {
                path: '/order',
                component: Order,
                meta: { needLogin: true, index: 2 },

            },{
                path: '/mine',
                component: Mine,
                meta: { needLogin: true, index: 3 },

            },{
                path: '/personal',
                component: Personal,
                meta: { needLogin: true, index: 3 },

            },{
                path: '/finishOrder',
                component: FinishOrder,
                meta: { needLogin: true, index: 3 },

            },{
                path:'agreement',
                component: Agreement,
                meta: { needLogin: true, index: 3 },
            }
        ]
    }
]

const router = createRouter({
    routes,
    history: createWebHistory(process.env.BASE_URL),
})
let hasPersonalInfo = false
router.beforeEach((to, from, next) => {
    if (!to.meta.needLogin) {
        next()
    } else {
        if (localStorage.userId) {
            if (!hasPersonalInfo) {
                hasPersonalInfo = true
                axios.post('http://localhost:6001/detail/member', { id: localStorage.userId }).then(res => {
                    const data = res.data
                    // console.log();
                    if (!data.success) {
                        return next('/login')
                    }
                    store.commit('setPersonal', data.result)
                    // console.log(data);
                    next()
                })
            } else {
                next()
            }

        } else {
            next('/login')
        }
    }
})



export default router